package edu.ipccenter.toster.ejb;

import com.sun.xml.ws.api.tx.at.Transactional;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashSet;

import edu.ipccenter.toster.Name;
import edu.ipccenter.toster.Suite;
import edu.ipccenter.toster.SuiteUtils;
import edu.ipccenter.toster.jpa.JPATestSuite;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "SuiteUtils")
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class SuiteUtilsBean implements SuiteUtils {

    @PersistenceContext(unitName = "TosterPU")
    private EntityManager em;

    @Override
    public Collection<Name> findByName(String keyText, int upperLimit) {
        Collection<Name> names = new HashSet<Name>();

        Query q = em.createQuery("select c from JPATestSuite c where c.suiteName LIKE :keyText");
        q.setParameter("keyText", '%' + keyText + '%');

        List<JPATestSuite> jtest = (List<JPATestSuite>) q.getResultList();

        for (int i = 0; i < jtest.size() && i < upperLimit; i++) {
            Name n = new Name();
            n.setID(jtest.get(i).getSuiteId());
            n.setName(jtest.get(i).getSuiteName());
            names.add(n);
        }


        return names;
    }

    @Override
    public Suite findByID(BigInteger id) {
        Query q = em.createQuery("select c from JPATestSuite c where c.suiteId =:myid");
        q.setParameter("myid", id);
      //  System.out.println("EM:" + em);
        Suite suite = null;
        try{
        JPATestSuite testsuite = (JPATestSuite) q.getSingleResult();

      
        try {
            InitialContext ic = new InitialContext();
            suite = (Suite) ic.lookup("java:global/Tost/Tost-ejb/SuiteBean");
        } catch (NamingException ex) {
            Logger.getLogger(SuiteUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
            
            
        }

        suite.setSuite(testsuite);
        }
        catch(javax.persistence.NoResultException ex){
            System.out.println("NO results");
            return new SuiteBean();
            
        }
        return suite;
    }

    @Override
    public Suite create(String name, String description, BigInteger parentID) {
        Suite suite = null;
        JPATestSuite su = new JPATestSuite();

        su.setSuiteName(name);
        su.setSuiteDesc(description);
        su.setParentSuite(parentID);
        em.merge(su);

        try {
            InitialContext ic = new InitialContext();
            suite = (Suite) ic.lookup("java:global/Tost/Tost-ejb/SuiteBean");
        } catch (NamingException ex) {
            Logger.getLogger(SuiteUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        suite.setSuite(su);


        return suite;
    }
}
